package com.example.school.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.school.dto.UserAddMoneyDto;
import com.example.school.mapper.OrdersMapper;
import com.example.school.mapper.UserMapper;
import com.example.school.pojo.Orders;
import com.example.school.pojo.Tariff;
import com.example.school.pojo.User;
import com.example.school.pojo.Water;
import com.example.school.service.TariffService;
import com.example.school.mapper.TariffMapper;
import com.example.school.utils.common.enums.Code;
import com.example.school.utils.common.result.Result;
import com.example.school.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
* @author 24354
* @description 针对表【tariff】的数据库操作Service实现
* @createDate 2024-03-16 19:11:40
*/
@Service
public class TariffServiceImpl extends ServiceImpl<TariffMapper, Tariff>
    implements TariffService{

    @Autowired
    private TariffMapper tariffMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public Result getTariffMoney() {
        Long userId = BaseContext.getCurrentId();
        User checkUser = userMapper.selectById(userId);
        if (userId==null||checkUser==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Tariff checkTariff = tariffMapper.selectOne(Wrappers.<Tariff>lambdaQuery()
                .eq(Tariff::getTariffUserId, userId));
        String money="0";
        if (checkTariff!=null) {
            money= checkTariff.getTariffMoney();
        }
        return Result.okResult(money);
    }

    @Override
    public Result addTariffMoney(UserAddMoneyDto dto) {

        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Long userId = BaseContext.getCurrentId();
        User checkUser = userMapper.selectById(userId);
        if (userId==null||checkUser==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        String message=checkUser.getStudentName();
        if (checkUser.getStudentName()==null&&checkUser.getStudentName().equals("")) {
            message+=checkUser.getOpenId();
        }
        Tariff checkTariff = tariffMapper.selectOne(Wrappers.<Tariff>lambdaQuery()
                .eq(Tariff::getTariffUserId, userId));
        Tariff tariff=new Tariff();
        Orders orders=new Orders();
        if (checkTariff==null) {
            tariff.setTariffUserId(userId);
            tariff.setTariffMoney(dto.getMoney());
            int checkSave = tariffMapper.insert(tariff);
            if (checkSave==0) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            orders= orders.builder()
                    .ordersUserId(userId)
                    .ordersPrice(dto.getMoney())
                    .ordersName("电费")
                    .ordersMessage("用户:"+message+"开通了电费账户并向电费充值"+ dto.getMoney()+"元")
                    .ordersMsec(System.currentTimeMillis())
                    .ordersTime(String.valueOf(LocalDateTime.now())).build();
            int checkOrders = ordersMapper.insert(orders);
            if (checkOrders==0) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }
        BigDecimal old=new BigDecimal(checkTariff.getTariffMoney());
        BigDecimal add=new BigDecimal(dto.getMoney());
        String newMoney= old.add(add).toString();
        tariff.setTariffId(checkTariff.getTariffId());
        tariff.setTariffMoney(newMoney);
        int checkUpdate = tariffMapper.updateById(tariff);
        if (checkUpdate==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        orders= orders.builder()
                .ordersUserId(userId)
                .ordersPrice(dto.getMoney())
                .ordersName("电费")
                .ordersMessage("用户:"+message+"向电费充值"+ dto.getMoney()+"元")
                .ordersMsec(System.currentTimeMillis())
                .ordersTime(String.valueOf(LocalDateTime.now())).build();
        int checkOrders = ordersMapper.insert(orders);
        if (checkOrders==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

}




